﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MedicalCare.DbLayer.Adaptors;
using StructureMap;
using MedicalCare.Messages;
using MedicalCare.EntityDefinitions.Entities;
using MedicalCare.Utilities;

namespace MedicalCare.Admin
{
    public partial class EquipmentTypeDetail : System.Web.UI.Page
    {
        private IEquipmentTypeAdaptor _eptAdaptor;
        private int equipmentTypeId { get { return Convert.ToInt32(ViewState["equipmentTypeId"]); } set { ViewState["equipmentTypeId"] = value; } }
        private string action { get { return Convert.ToString(ViewState["action"]); } set { ViewState["action"] = value; } }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                action = Request.Params["action"].ToString();
                if (ActionType.NEW.ToString().Equals(action))
                {
                    cleanPage();
                }
                else if (ActionType.EDIT.ToString().Equals(action))
                {
                    loadEquipmentTypeDetail();
                }
            }
        }

        private void cleanPage()
        {
                tbEquipmentName.Text = "";
                ttEquipmentComment.Value = "";
        }

        private void loadEquipmentTypeDetail()
        {
            _eptAdaptor = ObjectFactory.GetInstance<IEquipmentTypeAdaptor>();
            equipmentTypeId = Convert.ToInt32(Request.Params["Id"].ToString());
            EquipmentTypeEntity equipmentsEntity = _eptAdaptor.GetEquipmentById(equipmentTypeId);
            tbEquipmentName.Text = equipmentsEntity.Name.ToString();
            ttEquipmentComment.Value = equipmentsEntity.Comment.ToString();
        }

        private EquipmentTypeMessage GetEquipmentTypeMessage(string action)
        {
            EquipmentTypeMessage equipmentTypeMessage = null;
            if (ActionType.NEW.ToString().Equals(action))
            {
                equipmentTypeMessage = new EquipmentTypeMessage()
                {
                    Name = tbEquipmentName.Text.ToString(),
                    Comment = ttEquipmentComment.Value.ToString(),
                    CreateUser = AppPage.CurrentUser.UserName,
                    Valid = "Y"
                };
            }
            else if (ActionType.EDIT.ToString().Equals(action))
            {
                equipmentTypeMessage = new EquipmentTypeMessage()
                {
                    Id = equipmentTypeId,
                    Name = tbEquipmentName.Text.ToString(),
                    Comment = ttEquipmentComment.Value.ToString(),
                    ModifyUser = AppPage.CurrentUser.UserName,
                };
            }
            else
            {
            }

            return equipmentTypeMessage;
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            _eptAdaptor = ObjectFactory.GetInstance<IEquipmentTypeAdaptor>();
            var message = GetEquipmentTypeMessage(action);
            _eptAdaptor.Update(message);
            //if (ActionType.NEW.ToString().Equals(action))
            //{
            //    _eptAdaptor.AddEquipmentType(message);
            //}
            //else if (ActionType.EDIT.ToString().Equals(action))
            //{
            //    _eptAdaptor.EditEquipmentType(message);
            //}
            //else
            //{ }

            Server.Transfer("~/Admin/EquipmentTypeList.aspx");
        }

        protected void btCancel_Click(object sender, EventArgs e)
        {
            Server.Transfer("~/Admin/EquipmentTypeList.aspx");
        }
    }
}